package org.getOffer.offer33;

/**
 * @Author yu
 * @Date 2021/11/13
 */
public class Solution {

    public static void main(String[] args) {

        int[] nums = {1,3,2,6,5};

        Solution solution = new Solution();
        System.out.println(solution.verifyPostorder(nums));

    }

    public boolean verifyPostorder(int[] postorder) {
        return this.check(postorder, 0, postorder.length - 1);
    }

    public boolean check(int[] postorder, int left, int right) {
        if (left >= right) {
            return true;
        }
        //int p = left;
        while (postorder[left] < postorder[right]) {
            left++;
        }
        int m = left;
        while (postorder[left] > postorder[right]) {
            left++;
        }
        return left == right && this.check(postorder, 0, m-1) && this.check(postorder, m, right - 1);
    }

}
